package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 28792
 * Date: 2023-11-19
 * Time: 11:40
 */
public  class AddOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要添加的书名");
        String name = scanner.nextLine();
        System.out.println("请输入该书的作者");
        String author = scanner.nextLine();
        System.out.println("请输入该书的价格");
        int price = scanner.nextInt();
        System.out.println("请输入该书的类型");
        String type = scanner.nextLine();

        Book book = new Book(name,author,price,type);

        int currentSize = bookList.getUsedSize();
        for(int i = 0; i < currentSize;i++){
            Book tep = bookList.getBook(i);
            if(tep.getName().equals(name)){
                System.out.println("存在这本书,不可重复添加。 ");
                return;
            }
        }
        //没有重复的书 开始新增
        bookList.setBook(book,currentSize);
        bookList.setUsedSize(currentSize+1);
    }
}
